package com.athuiju.eduservice.service.impl;

import com.athuiju.eduservice.entity.EduCourse;
import com.athuiju.eduservice.entity.EduCourseDescription;
import com.athuiju.eduservice.entity.frontVo.CourseQueryVo;
import com.athuiju.eduservice.entity.frontVo.CourseWebVo;
import com.athuiju.eduservice.entity.vo.CourseInfoVo;
import com.athuiju.eduservice.entity.vo.CoursePublishVo;
import com.athuiju.eduservice.entity.vo.CourseQuery;
import com.athuiju.eduservice.mapper.EduCourseMapper;
import com.athuiju.eduservice.service.EduChapterService;
import com.athuiju.eduservice.service.EduCourseDescriptionService;
import com.athuiju.eduservice.service.EduCourseService;
import com.athuiju.eduservice.service.EduVideoService;
import com.athuiju.servicebase.exceptionHandler.HuiJuException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author wzh
 * @since 2020-06-01
 */
@Service("courseService")
public  class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService courseDescriptionService;

    //注入小节和章节service
    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private EduChapterService chapterService;



    @Override
    public void pageQuery(Page<EduCourse> pageParam, CourseQuery courseQuery) {
            //构造条件
            QueryWrapper<EduCourse> queryWrapper=new QueryWrapper<>();
            //根据sort正序排序
            //如果条件参数为空，就只根据分页查询，并return
            if ( courseQuery==null){
                baseMapper.selectPage(pageParam, queryWrapper);
                return;
            }
            String title = courseQuery.getTitle();
            Integer status = courseQuery.getStatus();
            if (!StringUtils.isEmpty(title)){
                queryWrapper.like("title", title);
            }
        System.out.println(title);
            if (!StringUtils.isEmpty(status)){
                System.out.println("-------------"+status);
                if (status==1){
                    queryWrapper.eq("status", "Normal");
                }else{
                    queryWrapper.eq("status", "Draft");
                }

            }
            queryWrapper.orderByDesc("gmt_create");
            baseMapper.selectPage(pageParam, queryWrapper);
        }

    //添加课程基本信息方法
    @Transactional
    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        //1.向课程表添加课程基本信息
        //CourseInfo对象转换eduCourse对象
        EduCourse eduCourse=new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        int insert = baseMapper.insert(eduCourse);
        if (insert ==0 ){
            //添加失败
            throw  new HuiJuException(20001,"添加课程信息失败");
        }
        //获取添加之后课程id
        String cid = eduCourse.getId();

        //2 向课程简介表添加课程简介
        //edu_course_description
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setDescription(courseInfoVo.getDescription());
        //设置描述id就是课程id
        courseDescription.setId(cid);
        courseDescriptionService.save(courseDescription);

        return cid;
    }

    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        //1 查询课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse,courseInfoVo);

        //2 查询描述表
        EduCourseDescription courseDescription = courseDescriptionService.getById(courseId);
        courseInfoVo.setDescription(courseDescription.getDescription());

        return courseInfoVo;
    }

    //修改课程信息
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        //1 修改课程表
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int update = baseMapper.updateById(eduCourse);
        if(update == 0) {
            throw new HuiJuException(20001,"修改课程信息失败");
        }

        //2 修改描述表
        EduCourseDescription description = new EduCourseDescription();
        description.setId(courseInfoVo.getId());
        description.setDescription(courseInfoVo.getDescription());
        courseDescriptionService.updateById(description);
    }

    //根据课程id查询课程确认信息
    @Override
    public CoursePublishVo publishCourseInfo(String id) {
        //调用mapper
        CoursePublishVo publishCourseInfo = baseMapper.selectCoursePublishVoById(id);
        return publishCourseInfo;
    }



    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        return baseMapper.getBaseCourseInfo(courseId);
    }

    @Override
    public void removeCourse(String courseId) {
       //1 根据课程id删除小节
        eduVideoService.removeVideoByCourseId(courseId);

        //2 根据课程id删除章节
        chapterService.removeChapterByCourseId(courseId);

        //3 根据课程id删除描述
        courseDescriptionService.removeById(courseId);

        //4 根据课程id删除课程本身
       int result = baseMapper.deleteById(courseId);
        if(result == 0) { //失败返回
            throw new HuiJuException(20001,"删除失败");
       }
    }

    @Override
    public Map<String, Object> pageListWeb(Page<EduCourse> pageParam, CourseQueryVo courseQuery) {

        QueryWrapper<EduCourse> queryWrapper=new QueryWrapper<>();
        if (!StringUtils.isEmpty(courseQuery.getSubjectParentId())){
            queryWrapper.eq("subject_parent_id",courseQuery.getSubjectParentId());
        }
        if (!StringUtils.isEmpty(courseQuery.getSubjectId())){
            queryWrapper.eq("subject_parent_id",courseQuery.getSubjectId());
        }
        if (!StringUtils.isEmpty(courseQuery.getBuyCountSort())){
            queryWrapper.eq("subject_parent_id",courseQuery.getBuyCountSort());
        }
        if (!StringUtils.isEmpty(courseQuery.getGmtCreateSort())){
            queryWrapper.eq("subject_parent_id",courseQuery.getGmtCreateSort());
        }
        if (!StringUtils.isEmpty(courseQuery.getPriceSort())){
            queryWrapper.eq("subject_parent_id",courseQuery.getPriceSort());
        }
        baseMapper.selectPage(pageParam,queryWrapper);
        List<EduCourse>records=pageParam.getRecords();
        long current=pageParam.getCurrent();
        long pages=pageParam.getPages();
        long size=pageParam.getSize();
        long total=pageParam.getTotal();
        boolean hasNext=pageParam.hasNext();
        boolean hasPrevious=pageParam.hasPrevious();
        Map<String,Object>map=new HashMap<String,Object>();
        map.put("records",records);
        map.put("current",current);
        map.put("size",size);
        map.put("pages",pages);
        map.put("total",total);
        map.put("hasNext",hasNext);
        map.put("hasPrevious",hasPrevious);
        return map;
    }

}